package com.myk.game.gobangchess.dates;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.myk.game.gobangchess.Account;
import com.myk.game.gobangchess.beans.PlayerAssetsBean;
import com.myk.game.gobangchess.beans.PlayerLoginBean;
import com.myk.game.gobangchess.beans.PlayerPlayingBean;
import com.myk.game.gobangchess.constants.ChatLevel;
import com.myk.game.gobangchess.constants.PathCost;
import com.myk.game.gobangchess.utils.DateTimeUtil;
import com.myk.game.gobangchess.utils.FileUtils;
import com.myk.game.gobangchess.utils.Pair;
import com.myk.game.gobangchess.utils.StringUtils;
import java.io.File;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;
import java.util.function.Predicate;
public class AccountData {


    /**
     * 根据idCode获取账户，如果账户不存在返回null
     */
    public static Account getAccountByIdCode(String idCode) {
        String accountsPath = PathCost.DATA_ACCOUNT_GROUP_DIR;
        FileUtils.createOrExistsDir(accountsPath);
        List<File> accountDirList = FileUtils.listFilesInDir(accountsPath);
        for (File file : accountDirList) {
            String dirName = FileUtils.getFileNameNoExtension(file);
            String[] dirNameData = dirName.split("_");
            if (idCode.equals(dirNameData[0])) {
                String mainFile = accountsPath + dirName + "/" + PathCost.ACCOUNT_MAIN_FILE;
                if (FileUtils.isFileExists(mainFile)) {
                    String accountMainJson = FileUtils.readFile2String(mainFile);
                    Gson gson = new Gson();
                    Account account = gson.fromJson(accountMainJson, Account.class);
                    return account;
                }
                else {
                    return null;
                }
            }
        }
        return null;
    }

    /**
     * 根据nickname获取账号信息
     */
    public static Account getAccountByNickName(String nickName) {
        String accountsPath = PathCost.DATA_ACCOUNT_GROUP_DIR;
        FileUtils.createOrExistsDir(accountsPath);
        List<File> accountDirList = FileUtils.listFilesInDir(accountsPath);
        for (File file : accountDirList) {
            String dirName = FileUtils.getFileNameNoExtension(file);
            String[] dirNameData = dirName.split("_");
            if (nickName.equals(dirNameData[1])) {
                String mainFile = accountsPath + dirName + "/" + PathCost.ACCOUNT_MAIN_FILE;
                if (FileUtils.isFileExists(mainFile)) {
                    String accountMainJson = FileUtils.readFile2String(mainFile);
                    Gson gson = new Gson();
                    Account account = gson.fromJson(accountMainJson, Account.class);
                    return account;
                }
                else {
                    return null;
                }
            }
        }
        return null;
    }

    /**
     * 判断idCode是否已经存在
     */
    public static boolean isIdCodeAccess(String idCode) {
        String accountsPath = PathCost.DATA_ACCOUNT_GROUP_DIR;
        FileUtils.createOrExistsDir(accountsPath);
        List<File> accountDirList = FileUtils.listFilesInDir(accountsPath);
        for (File file : accountDirList) {
            if (idCode.equals(FileUtils.getFileNameNoExtension(file).split("_")[0])) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断nickName是否已经存在，不需要比较临时昵称，因为临时昵称的格式和一般昵称不一样
     */
    public static boolean isNicknameAccess(String nickName) {
        String accountsPath = PathCost.DATA_ACCOUNT_GROUP_DIR;
        FileUtils.createOrExistsDir(accountsPath);
        List<File> accountDirList = FileUtils.listFilesInDir(accountsPath);
        for (File file : accountDirList) {
            if (nickName.equals(FileUtils.getFileNameNoExtension(file).split("_")[1])) {
                return true;
            }
        }
        return false;
    }

    /**
     * 获取当前新注册未审核的账号数量
     */
    public static int getRegisterCount() {
        int count = 0;
        String accountsPath = PathCost.DATA_ACCOUNT_GROUP_DIR;
        FileUtils.createOrExistsDir(accountsPath);
        List<File> accountDirList = FileUtils.listFilesInDir(accountsPath);
        for (File file : accountDirList) {
            if (FileUtils.isFileExists(accountsPath + file.getName() + "/" + PathCost.ACCOUNT_REGISTER_FILE)) {
                count++;
            }
        }
        return count;
    }

    /**
     * 判断该临时昵称是否已经存在
     */
    public static boolean isTempNicknameExist(String tempNickName) {
        String accountsPath = PathCost.DATA_ACCOUNT_GROUP_DIR;
        FileUtils.createOrExistsDir(accountsPath);
        List<File> accountDirList = FileUtils.listFilesInDir(accountsPath);
        for (File file : accountDirList) {
            String registerFilePath = accountsPath + file.getName() + "/" + PathCost.ACCOUNT_REGISTER_FILE;
            if (FileUtils.isFileExists(registerFilePath)) {
                String thisNickName = FileUtils.readFile2String(registerFilePath);
                if (tempNickName.equals(thisNickName)) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 生成临时昵称 @临时昵称A ... Z
     */
    public static String generateTempNickName() {
        String[] tempNickNames = new String[26];  //26个大写英文字母，保证大于AppConfig.register_max_count限定的审核中账号数量
        for (int i = 0; i < tempNickNames.length; i++) {
            tempNickNames[i] = "@临时昵称" + ((char) (65 + i));
        }
        String aTemp = tempNickNames[(int) (Math.random() * tempNickNames.length)];
        if (AccountData.isTempNicknameExist(aTemp)) {
            return generateTempNickName();
        }
        else {
            return aTemp;
        }
    }

    /**
     * 判断该账号是否审核中状态
     */
    public static boolean isAccountUnderReview(Account account) {
        String accountsPath = PathCost.DATA_ACCOUNT_GROUP_DIR;
        String accountDir = accountsPath + account.getIdCode() + "_" + account.getNickName() + "/";
        String registerFile = accountDir + PathCost.ACCOUNT_REGISTER_FILE;
        return FileUtils.isFileExists(registerFile);
    }

    /**
     * 判断该账号是否被封禁状态
     */
    public static boolean isAccountUnderBan(Account account) {
        String accountsPath = PathCost.DATA_ACCOUNT_GROUP_DIR;
        String accountDir = accountsPath + account.getIdCode() + "_" + account.getNickName() + "/";
        String registerFile = accountDir + PathCost.ACCOUNT_BAN_FILE;
        return FileUtils.isFileExists(registerFile);
    }

    /**
     * 如果用户是审核中状态返回其临时昵称否则返回正常昵称
     */
    public static String getNickNameOrTemp(Account account) {
        if (isAccountUnderReview(account)) {
            String accountsPath = PathCost.DATA_ACCOUNT_GROUP_DIR;
            String accountDir = accountsPath + account.getIdCode() + "_" + account.getNickName() + "/";
            String registerFile = accountDir + PathCost.ACCOUNT_REGISTER_FILE;
            return FileUtils.readFile2String(registerFile);
        }
        else {
            return account.getNickName();
        }
    }

    /**
     * 设置该账号为审核状态，插入标记为新注册账户的的标记文件，文件中保存临时昵称
     */
    public static void setAccountUnderCheck(String idCode, String nickName, String tempNickName) {
        String accountsPath = PathCost.DATA_ACCOUNT_GROUP_DIR;
        String accountDir = accountsPath + idCode + "_" + nickName + "/";
        String registerFile = accountDir + PathCost.ACCOUNT_REGISTER_FILE;
        boolean isSuccess = FileUtils.createFileByDeleteOldFile(registerFile); //创建标记新注册账号的文件
        if (isSuccess) {
            FileUtils.writeFileFromString(registerFile, tempNickName, false); //将临时昵称存到标记文件中
        }
    }

    /**
     * 设置该账号通过审核，删除标记新注册账号的文件
     */
    public static void setAccountPassCheck(Account account) {
        String accountsPath = PathCost.DATA_ACCOUNT_GROUP_DIR;
        String accountDir = accountsPath + account.getIdCode() + "_" + account.getNickName() + "/";
        String registerFile = accountDir + PathCost.ACCOUNT_REGISTER_FILE;
        FileUtils.delete(registerFile); //删除标记新注册账号的文件
    }

    /**
     * 设置该账号拒绝审核，直接删除整个账号文件夹
     */
    public static void setAccountRejectCheck(Account account) {
        String accountsPath = PathCost.DATA_ACCOUNT_GROUP_DIR;
        String accountDir = accountsPath + account.getIdCode() + "_" + account.getNickName() + "/";
        String registerFile = accountDir + PathCost.ACCOUNT_REGISTER_FILE;
        FileUtils.delete(registerFile); //删除标记新注册账号的文件
        String banFile = accountDir + PathCost.ACCOUNT_BAN_FILE;
        FileUtils.createFileByDeleteOldFile(banFile); //创建标记封禁该账号的文件
    }

    /**
     * 获取用户角色信息
     */
    public static String getPlayerInfo(Account account) {
        Gson gson = new Gson();

        int chatLevel = 0;
        String chatTip = "-1";
        String otherUserinfo = "";
        String badgeInfo = "";
        String winInfo = "";
        String loginInfo = "";
        String amountInfo = "";

        String loginFile = PathCost.DATA_ACCOUNT_GROUP_DIR + account.getIdCode() + "_" + account.getNickName() + "/" + PathCost.ACCOUNT_LOGIN_FILE;
        String assetsFile = PathCost.DATA_ACCOUNT_GROUP_DIR + account.getIdCode() + "_" + account.getNickName() + "/" + PathCost.ACCOUNT_ASSETS_FILE;
        String flowFile = PathCost.DATA_ACCOUNT_GROUP_DIR + account.getIdCode() + "_" + account.getNickName() + "/" + PathCost.AMOUNT_FLOW_FILE;
        String playingFile = PathCost.DATA_ACCOUNT_GROUP_DIR + account.getIdCode() + "_" + account.getNickName() + "/" + PathCost.ACCOUNT_PLAYING_FILE;

        String loginJson = FileUtils.readFile2String(loginFile);
        String assetsJson = FileUtils.readFile2String(assetsFile);
        String playingJson = FileUtils.readFile2String(playingFile);

        if (!StringUtils.isSpace(loginJson)) {
            PlayerLoginBean loginBean = gson.fromJson(loginJson, PlayerLoginBean.class);
            loginInfo += "您已登录{{" + loginBean.getLoginDayCount() + "@scale=1.5}}天";
            otherUserinfo += loginInfo;
        }

        if (!StringUtils.isSpace(assetsJson)) {
            PlayerAssetsBean playerAssetsBean = gson.fromJson(assetsJson, PlayerAssetsBean.class);
            chatLevel = playerAssetsBean.getChatLevel();
            if (chatLevel == ChatLevel.PAY.getLevel()) {
                chatTip = ("您可以消耗一点代币输入自定义消息发送");
            }
            else if (chatLevel == ChatLevel.FREE.getLevel()) {
                chatTip = ("您可以自由的输入自定义消息发送");
            }
            else if (chatLevel == ChatLevel.BAN.getLevel()) {
                chatTip = ("您已被禁言暂时不可以发送消息");
            }
            else {
                chatTip = ("您可以点击右下角图标选择预设消息发送");
            }
            if (FileUtils.isFileExists(flowFile)) {
                amountInfo += "，代币余额{{" + playerAssetsBean.getAmount() + "@scale=1.5}}元。";
                otherUserinfo += amountInfo;
            }
            else {
                otherUserinfo += "。";
            }
            String badge = playerAssetsBean.getBadge();
            if (badge != null && !badge.equals("")) {
                badgeInfo += "\n\uD83D\uDC51【" + badge + "】，";
                otherUserinfo += badgeInfo;
            }
        }

        if (!StringUtils.isSpace(playingJson)) {
            PlayerPlayingBean playingBean = gson.fromJson(playingJson, PlayerPlayingBean.class);
            long playingCount = 0;
            long winCount = 0;
            for (PlayerPlayingBean.ChessPlayingBean chessPlayingBean : playingBean.getChessPlayingList()) {
                playingCount += chessPlayingBean.getPlayingCount();
                winCount += chessPlayingBean.getWinCount();
            }
            if (badgeInfo.equals("")) {
                winInfo += "\n";
            }
            winInfo += "总计对弈{{" + playingCount + "@scale=1.5}}场，获胜{{" + winCount + "@scale=1.5}}次。";
            otherUserinfo += winInfo;
        }
        return "idCode:" + account.getIdCode() + "&" + "nickName:" + AccountData.getNickNameOrTemp(account) + "&" + "chatLevel:" + chatLevel + "&" + "chatTip:" + chatTip + "&" + "otherUserinfo:" + otherUserinfo;
    }

    /**
     * 获取用户聊天等级
     */
    public static String getChatLevel(Account account) {
        String assetsFile = PathCost.DATA_ACCOUNT_GROUP_DIR + account.getIdCode() + "_" + account.getNickName() + "/" + PathCost.ACCOUNT_ASSETS_FILE;
        if (FileUtils.isFileExists(assetsFile)){
            try {
                String assetsJson = FileUtils.readFile2String(assetsFile);
                Gson gson = new Gson();
                PlayerAssetsBean playerAssetsBean = gson.fromJson(assetsJson, PlayerAssetsBean.class);
                return playerAssetsBean.getChatLevel() + "," + ChatLevel.getDescByLevel(playerAssetsBean.getChatLevel());
            }
            catch (JsonSyntaxException e) {
                System.out.println("assetsJson解析异常：" + e);
                return ChatLevel.PRESET.getLevel() + "," + ChatLevel.PRESET.getDesc(); //为异常返回"0,预设"
            }
        }
        return ChatLevel.PRESET.getLevel() + "," + ChatLevel.PRESET.getDesc(); //为空默认返回"0,预设"
    }

    /**
     * 更新用户最近登录时间
     */
    public static void updatePlayerLoginTime(Account account) {
        Gson gson = new Gson();
        String loginFile = PathCost.DATA_ACCOUNT_GROUP_DIR + account.getIdCode() + "_" + account.getNickName() + "/" + PathCost.ACCOUNT_LOGIN_FILE; //获取当前用户登录信息文件
        if (FileUtils.isFileExists(loginFile)) { //若登录信息文件存在
            String loginData = FileUtils.readFile2String(loginFile);
            PlayerLoginBean oldLoginBean = gson.fromJson(loginData, PlayerLoginBean.class); //获取本地保存的登录信息数据
            LocalDateTime nowDateTime = LocalDateTime.now(); //当前登录时间

            PlayerLoginBean newLoginBean = new PlayerLoginBean();
            newLoginBean.setRecentLoginTime(DateTimeUtil.localDateTimeToStr(nowDateTime, DateTimeUtil.ALL_FORMAT)); //更新最近登录时间

            String lastCountDate = oldLoginBean.getLastCountDate(); //获取统计日期
            if (lastCountDate == null || "".equals(lastCountDate)) { //数据有问题的话修正数据
                lastCountDate = DateTimeUtil.localDateTimeToStr(nowDateTime, DateTimeUtil.DATE_FORMAT);
                oldLoginBean.setLastCountDate(lastCountDate);
            }
            LocalDateTime lastCountDateTime = DateTimeUtil.strToLocalDateTime(lastCountDate + " 00:00:00", DateTimeUtil.ALL_FORMAT); //获取用于计算天数间隔的旧登录时间
            if (DateTimeUtil.betweenDay(lastCountDateTime, nowDateTime) > 0) { //登录间隔时间超过一天
                newLoginBean.setLastCountDate(DateTimeUtil.localDateTimeToStr(nowDateTime, DateTimeUtil.DATE_FORMAT)); //更新旧日期
                newLoginBean.setLoginDayCount(oldLoginBean.getLoginDayCount() + 1); //登录天数加1
                updateGlobalLoginsData(account); //全局记录今日新登用户
            }
            else { ////登录间隔不超过一天，保留原数据
                newLoginBean.setLastCountDate(oldLoginBean.getLastCountDate());
                newLoginBean.setLoginDayCount(oldLoginBean.getLoginDayCount());
            }
            String json = gson.toJson(newLoginBean);
            FileUtils.writeFileFromString(loginFile, json);  //更新登录信息文件
        }
        else { //若登录信息文件不存在，创建文件并且存初始化数据
            PlayerLoginBean loginBean = new PlayerLoginBean();
            LocalDateTime now = LocalDateTime.now();
            loginBean.setRecentLoginTime(DateTimeUtil.localDateTimeToStr(now, DateTimeUtil.ALL_FORMAT));
            loginBean.setLastCountDate(DateTimeUtil.localDateTimeToStr(now, DateTimeUtil.DATE_FORMAT));
            loginBean.setLoginDayCount(1);
            updateGlobalLoginsData(account);//全局记录今日新登用户
            String json = gson.toJson(loginBean);
            FileUtils.createOrExistsFile(loginFile);
            FileUtils.writeFileFromString(loginFile, json);
        }
    }

    /**
     * 全局记录今日新登录用户
     */
    public static void updateGlobalLoginsData(Account account) {
        FileUtils.createOrExistsDir(PathCost.GLOBAL_DATA_STATISTICAL_DIR);
        FileUtils.createOrExistsDir(PathCost.GLOBAL_LOGINS_DATA_DIR);
        String toDayDate = DateTimeUtil.localDateTimeToStr(LocalDateTime.now(), DateTimeUtil.DATE_FORMAT);
        String toDayFile = PathCost.GLOBAL_LOGINS_DATA_DIR + toDayDate + ".txt";
        if (FileUtils.isFileExists(toDayFile)) {
            FileUtils.writeFileFromString(toDayFile, "，" + account.getNickName(), true);
        }
        else {
            FileUtils.createOrExistsFile(toDayFile);
            FileUtils.writeFileFromString(toDayFile, account.getNickName());
        }
    }

    /**
     * 更新用户联系方式
     */
    public static void updatePlayerContact(String idCode, String nickName, String contact) {
        Gson gson = new Gson();
        String accountsPath = PathCost.DATA_ACCOUNT_GROUP_DIR;
        String accountDir = accountsPath + idCode + "_" + nickName + "/";
        String assetsFile = accountDir + PathCost.ACCOUNT_ASSETS_FILE;//获取当前用户资产信息文件
        if (FileUtils.isFileExists(assetsFile)) { //若资产信息文件存在
            String assetsData = FileUtils.readFile2String(assetsFile);
            PlayerAssetsBean assetsBean = gson.fromJson(assetsData, PlayerAssetsBean.class); //获取本地保存的资产信息数据
            assetsBean.setContact(contact); //设置新联系方式
            String json = gson.toJson(assetsBean);
            FileUtils.writeFileFromString(assetsFile, json);  //更新资产信息文件
        }
        else { //若资产信息文件不存在，创建文件并且存初始化数据
            PlayerAssetsBean assetsBean = new PlayerAssetsBean();
            assetsBean.setContact(contact);
            assetsBean.setBadge("");
            assetsBean.setAmount(0);
            assetsBean.setChatLevel(ChatLevel.PRESET.getLevel());
            String json = gson.toJson(assetsBean);
            FileUtils.createOrExistsFile(assetsFile);
            FileUtils.writeFileFromString(assetsFile, json);
        }
    }

    /**
     * 更新用户荣誉标签
     */
    public static void updatePlayerBadge(String idCode, String nickName, String badge) {
        Gson gson = new Gson();
        String accountsPath = PathCost.DATA_ACCOUNT_GROUP_DIR;
        String accountDir = accountsPath + idCode + "_" + nickName + "/";
        String assetsFile = accountDir + PathCost.ACCOUNT_ASSETS_FILE;//获取当前用户资产信息文件
        if (FileUtils.isFileExists(assetsFile)) { //若资产信息文件存在
            String assetsData = FileUtils.readFile2String(assetsFile);
            PlayerAssetsBean assetsBean = gson.fromJson(assetsData, PlayerAssetsBean.class); //获取本地保存的资产信息数据
            assetsBean.setBadge(badge);
            String json = gson.toJson(assetsBean);
            FileUtils.writeFileFromString(assetsFile, json);  //更新资产信息文件
        }
        else { //若资产信息文件不存在，创建文件并且存初始化数据
            PlayerAssetsBean assetsBean = new PlayerAssetsBean();
            assetsBean.setContact("");
            assetsBean.setBadge(badge);
            assetsBean.setAmount(0);
            assetsBean.setChatLevel(ChatLevel.PRESET.getLevel());
            String json = gson.toJson(assetsBean);
            FileUtils.createOrExistsFile(assetsFile);
            FileUtils.writeFileFromString(assetsFile, json);
        }
    }

    /**
     * 更新用户聊天权限等级
     */
    public static void updatePlayerChatLevel(String idCode, String nickName, int chatLevel) {
        Gson gson = new Gson();
        String accountsPath = PathCost.DATA_ACCOUNT_GROUP_DIR;
        String accountDir = accountsPath + idCode + "_" + nickName + "/";
        String assetsFile = accountDir + PathCost.ACCOUNT_ASSETS_FILE;//获取当前用户资产信息文件
        if (FileUtils.isFileExists(assetsFile)) { //若资产信息文件存在
            String assetsData = FileUtils.readFile2String(assetsFile);
            PlayerAssetsBean assetsBean = gson.fromJson(assetsData, PlayerAssetsBean.class); // 获取本地保存的资产信息数据
            assetsBean.setChatLevel(chatLevel);
            String json = gson.toJson(assetsBean);
            FileUtils.writeFileFromString(assetsFile, json);
        }
        else { //若资产信息文件不存在，创建文件并且存初始化数据
            PlayerAssetsBean assetsBean = new PlayerAssetsBean();
            assetsBean.setContact("");
            assetsBean.setBadge("");
            assetsBean.setAmount(0);
            assetsBean.setChatLevel(chatLevel);
            String json = gson.toJson(assetsBean);
            FileUtils.createOrExistsFile(assetsFile);
            FileUtils.writeFileFromString(assetsFile, json);
        }
    }

    /**
     * 管理员创建新账号
     */
    public static boolean managerCreateAccount(String idCode, String nickName, String createTime) {
        String accountsPath = PathCost.DATA_ACCOUNT_GROUP_DIR;
        FileUtils.createOrExistsDir(accountsPath); //创建账户全文件夹
        String accountDir = accountsPath + idCode + "_" + nickName + "/";
        FileUtils.createOrExistsDir(accountDir); //每个账户单独创建一个文件夹
        String accountFile = accountDir + PathCost.ACCOUNT_MAIN_FILE;
        boolean isSuccess = FileUtils.createFileByDeleteOldFile(accountFile); //创建主账号文件
        if (isSuccess) {
            Account account = new Account(idCode, nickName, createTime);
            String accountData = new Gson().toJson(account);
            return FileUtils.writeFileFromString(accountFile, accountData, false); //将主商户数据写入主账号文件中
        }
        return false;
    }

    /**
     * 获取账号列表信息
     */
    public static String getAccountListInfo(Predicate<Account> predicate) {
        //Gson gson = new Gson();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
        StringBuilder sb = new StringBuilder();
        String accountsPath = PathCost.DATA_ACCOUNT_GROUP_DIR;
        FileUtils.createOrExistsDir(accountsPath);
        List<File> accountDirList = FileUtils.listFilesInDir(accountsPath);
        List<Account> accountList = new ArrayList<>();
        for (File file : accountDirList) {
            //String mainPath = PathCost.DATA_ACCOUNT_GROUP_DIR + file.getName() + "/" + PathCost.ACCOUNT_MAIN_FILE;
            //String accountData = FileUtils.readFile2String(mainPath);
            //Account account = gson.fromJson(accountData, Account.class);
            String fileStr = file.getName();
            String[] strs = fileStr.split("_");
            Date date = new Date(file.lastModified());
            String createTime = sdf.format(date);
            Account account = new Account(strs[0], strs[1], createTime);
            if (predicate.test(account)) {
                accountList.add(account);
            }
        }
        for (Account account : accountList) {
            String accountState = "正常";
            if (AccountData.isAccountUnderReview(account)) {
                accountState = "审核";
            }
            else if (AccountData.isAccountUnderBan(account)) {
                accountState = "封禁";
            }
            //++日后还能扩展一些 活跃，冷淡，等等状态
            sb.append(account.toDataString())
                    .append("&")
                    .append(accountState)
                    .append(";");
        }
        String s = sb.toString();
        if (s.isEmpty()) {
            return "-1";
        }
        else {
            return s.substring(0, s.length() - 1);
        }
    }


    /**
     * 获取账号详细信息
     */
    public static String getAccountDetail(Account account) {
        String idCode = account.getIdCode();
        String nickName = account.getNickName();
        String tempName = AccountData.isAccountUnderReview(account) ? AccountData.getNickNameOrTemp(account) : "";
        String createTime = account.getCreateTime();
        String contact = "";
        String badge = "";
        String recentLoginTime = "";
        String loginDayCount = "";

        Gson gson = new Gson();
        String loginFile = PathCost.DATA_ACCOUNT_GROUP_DIR + account.getIdCode() + "_" + account.getNickName() + "/" + PathCost.ACCOUNT_LOGIN_FILE;
        if (FileUtils.isFileExists(loginFile)) {
            String loginBeanJson = FileUtils.readFile2String(loginFile);
            PlayerLoginBean loginBean = gson.fromJson(loginBeanJson, PlayerLoginBean.class);
            if (loginBean != null) {
                recentLoginTime = loginBean.getRecentLoginTime();
                loginDayCount = String.valueOf(loginBean.getLoginDayCount());
            }
        }
        String assetsFile = PathCost.DATA_ACCOUNT_GROUP_DIR + account.getIdCode() + "_" + account.getNickName() + "/" + PathCost.ACCOUNT_ASSETS_FILE;
        if (FileUtils.isFileExists(assetsFile)) {
            String assetsBeanJson = FileUtils.readFile2String(assetsFile);
            PlayerAssetsBean assetsBean = gson.fromJson(assetsBeanJson, PlayerAssetsBean.class);
            if (assetsBean != null) {
                contact = assetsBean.getContact();
                badge = assetsBean.getBadge();
            }
        }
        String detail = "账号：" + idCode + "\n" +
                "昵称：" + nickName + "\n" +
                "临时昵称：" + tempName + "\n" +
                "创建日期：" + createTime + "\n" +
                "联系方式：" + contact + "\n" +
                "荣誉标签：" + badge + "\n" +
                "最近登录时间：" + recentLoginTime + "\n" +
                "登录天数：" + loginDayCount;
        return account.getIdCode() + "&" + account.getNickName() + "&" + detail;
    }

    /**
     * 获取个人完整详细信息
     * “-1”表示暂无统计数据
     */
    public static String getPersonalCompletelyInfo(Account account) {
        String idCode = account.getIdCode(); //idCode
        String nickName = account.getNickName(); //昵称
        String badge = "-1"; //荣誉标签
        String amount = "0"; //代币余额
        String loginDayCount = "0"; //登录天数
        String playedChessNumber = "-1";  //玩过的棋的种类
        String playingPlayerNumber = "-1"; //对弈过的玩家数量
        String playingCount = "0"; //玩过的棋局数
        String winCount = "0"; //赢的次数
        String recentPlayingChess = "-1";//最近下的棋
        String recentPlayingPlayer = "-1"; //最近对弈的玩家
        String recentPlayingTime = "-1";//最近对弈时间
        String createTime = account.getCreateTime(); //账号创建日期

        Gson gson = new Gson();
        String loginFile = PathCost.DATA_ACCOUNT_GROUP_DIR + account.getIdCode() + "_" + account.getNickName() + "/" + PathCost.ACCOUNT_LOGIN_FILE;
        String assetsFile = PathCost.DATA_ACCOUNT_GROUP_DIR + account.getIdCode() + "_" + account.getNickName() + "/" + PathCost.ACCOUNT_ASSETS_FILE;
        String flowFile = PathCost.DATA_ACCOUNT_GROUP_DIR + account.getIdCode() + "_" + account.getNickName() + "/" + PathCost.AMOUNT_FLOW_FILE;
        String playingFile = PathCost.DATA_ACCOUNT_GROUP_DIR + account.getIdCode() + "_" + account.getNickName() + "/" + PathCost.ACCOUNT_PLAYING_FILE;
        String loginJson = FileUtils.readFile2String(loginFile);
        String assetsJson = FileUtils.readFile2String(assetsFile);
        String playingJson = FileUtils.readFile2String(playingFile);

        if (!StringUtils.isSpace(loginJson)) {
            PlayerLoginBean loginBean = gson.fromJson(loginJson, PlayerLoginBean.class);
            loginDayCount = String.valueOf(loginBean.getLoginDayCount());
        }
        if (!StringUtils.isSpace(assetsJson)) {
            PlayerAssetsBean playerAssetsBean = gson.fromJson(assetsJson, PlayerAssetsBean.class);
            badge = playerAssetsBean.getBadge();
            if (FileUtils.isFileExists(flowFile)) {
                amount = String.valueOf(playerAssetsBean.getAmount());
            }
        }

        if (!StringUtils.isSpace(playingJson)) {
            PlayerPlayingBean playingBean = gson.fromJson(playingJson, PlayerPlayingBean.class);
            long playing = 0;
            long win = 0;
            for (PlayerPlayingBean.ChessPlayingBean chessPlayingBean : playingBean.getChessPlayingList()) {
                playing += chessPlayingBean.getPlayingCount();
                win += chessPlayingBean.getWinCount();
            }
            playingCount = String.valueOf(playing);
            winCount = String.valueOf(win);
        }

        if (badge.equals("-1")) badge = "";
        if (playedChessNumber.equals("-1")) playedChessNumber = "";
        if (playingPlayerNumber.equals("-1")) playingPlayerNumber = "";
        if (recentPlayingChess.equals("-1")) recentPlayingChess = "";
        if (recentPlayingPlayer.equals("-1")) recentPlayingPlayer = "";
        if (recentPlayingTime.equals("-1")) recentPlayingTime = "";
        String text = "idCode:\t\t\t\t\t\t\t" + "{{********@hide=" + idCode + "}}" + "\t\t\t\t\t(请勿泄露)\n";
        text += "昵称:\t\t\t\t\t\t\t\t\t" + "{{" + nickName + "@back=#AA9933@style=i}}" + "\n";
        text += "荣誉标签:\t\t\t\t\t\t\t" + badge + "\n";
        text += "代币余额:\t\t\t\t\t\t\t" + amount + "\n";
        text += "登录天数:\t\t\t\t\t\t\t" + loginDayCount + "\n";
        text += "对弈棋类总数:\t\t\t" + playedChessNumber + "\n";
        text += "对弈玩家总数:\t\t\t" + playingPlayerNumber + "\n";
        text += "总对弈次数:\t\t\t\t\t\t" + playingCount + "\n";
        text += "总获胜次数:\t\t\t\t\t\t" + winCount + "\n";
        text += "最近对弈的棋:\t\t\t" + recentPlayingChess + "\n";
        text += "最近对弈玩家:\t\t\t" + recentPlayingPlayer + "\n";
        text += "最近对弈时间:\t\t\t" + recentPlayingTime + "\n";
        text += "账号注册时间:\t\t\t" + "{{" + createTime + "@back=#AA9933@style=i}}";
        return text;
    }

    /**
     * 按照对弈次数总记来排，顺便显示玩过的棋的种数，还要显示对弈过的玩家数量（暂时无法实现）
     * 没有产生过对弈的就不显示了，只下过一款棋的也不显示
     */
    public static List<Pair<Account, Pair<Integer, Long>>> getAccountListInfoOrderByPlayedCount() {
        Gson gson = new Gson();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
        String accountsPath = PathCost.DATA_ACCOUNT_GROUP_DIR;
        FileUtils.createOrExistsDir(accountsPath);
        List<File> accountDirList = FileUtils.listFilesInDir(accountsPath);
        List<Account> accountList = new ArrayList<>();
        for (File file : accountDirList) {
            String fileStr = file.getName();
            String[] strs = fileStr.split("_");
            Date date = new Date(file.lastModified());
            String createTime = sdf.format(date);
            Account account = new Account(strs[0], strs[1], createTime);
            if (!AccountData.isAccountUnderBan(account) && !AccountData.isAccountUnderReview(account)) {
                accountList.add(account); // 过滤掉被封禁和审核中的账号
            }
        }
        List<Pair<Account, Pair<Integer, Long>>> pairList = new ArrayList<>(); // 玩家，棋的种类，获胜总次数
        for (Account account : accountList) {
            if (FileUtils.isFileExists(PathCost.DATA_ACCOUNT_GROUP_DIR + account.getIdCode() + "_" + account.getNickName() + "/" + PathCost.ACCOUNT_PLAYING_FILE)) {
                String playingFile = PathCost.DATA_ACCOUNT_GROUP_DIR + account.getIdCode() + "_" + account.getNickName() + "/" + PathCost.ACCOUNT_PLAYING_FILE;
                String playingJson = FileUtils.readFile2String(playingFile);
                if (!StringUtils.isSpace(playingJson)) {
                    PlayerPlayingBean playingBean = gson.fromJson(playingJson, PlayerPlayingBean.class);
                    int playedChessNumber = playingBean.getChessPlayingList().size();
                    long playing = 0;
                    for (PlayerPlayingBean.ChessPlayingBean chessPlayingBean : playingBean.getChessPlayingList()) {
                        playing += chessPlayingBean.getPlayingCount();
                    }
                    if (playedChessNumber > 1) {
                        pairList.add(Pair.create(account, Pair.create(playedChessNumber, playing)));
                    }
                }
            }
        }
        pairList.sort((o1, o2) -> o2.second.second.compareTo(o1.second.second));
        return pairList;
    }


    /**
     * 获取该日登录过的用户列表
     */
    public static String getThisDayLoginPlayers(String date) {
        String file = PathCost.GLOBAL_LOGINS_DATA_DIR + date + ".txt";
        if (FileUtils.isFileExists(file)) {
            String data = FileUtils.readFile2String(file);
            if (StringUtils.isSpace(data)) {
                return "-1";
            }
            else {
                return data;
            }
        }
        else {
            return "-1";
        }
    }


    /**
     * 今天登录过的用户数量查询缓存
     */
    public static int todayLoginPlayersCountCache = 0;

    /**
     * (只在用户登录时调用)获取今日登录过的用户数量并且缓存到这里
     */
    public static int getTodayLoginPlayersCountAndCached() {
        String date = DateTimeUtil.localDateTimeToStr(LocalDateTime.now(), DateTimeUtil.DATE_FORMAT);
        String file = PathCost.GLOBAL_LOGINS_DATA_DIR + date + ".txt";
        if (FileUtils.isFileExists(file)) {
            String data = FileUtils.readFile2String(file);
            if (StringUtils.isSpace(data)) {
                return 0;
            }
            else {
                todayLoginPlayersCountCache = data.split("，").length;
                return todayLoginPlayersCountCache;
            }
        }
        else {
            return 0;
        }
    }

    /**
     * 获取他人资料
     */
    public static String getOtherPlayerInfo(Account account) {
        String nickName = account.getNickName();
        String createTime = account.getCreateTime();
        String badge = "";
        String loginDayCount = "";
        String allPlayingCount = "";
        String maxPlayingChess = "";

        Gson gson = new Gson();
        String loginFile = PathCost.DATA_ACCOUNT_GROUP_DIR + account.getIdCode() + "_" + account.getNickName() + "/" + PathCost.ACCOUNT_LOGIN_FILE;
        String assetsFile = PathCost.DATA_ACCOUNT_GROUP_DIR + account.getIdCode() + "_" + account.getNickName() + "/" + PathCost.ACCOUNT_ASSETS_FILE;
        String playingFile = PathCost.DATA_ACCOUNT_GROUP_DIR + account.getIdCode() + "_" + account.getNickName() + "/" + PathCost.ACCOUNT_PLAYING_FILE;
        if (FileUtils.isFileExists(assetsFile)) {
            String assetsBeanJson = FileUtils.readFile2String(assetsFile);
            PlayerAssetsBean assetsBean = gson.fromJson(assetsBeanJson, PlayerAssetsBean.class);
            if (assetsBean != null) {
                badge = assetsBean.getBadge();
            }
        }
        if (FileUtils.isFileExists(loginFile)) {
            String loginBeanJson = FileUtils.readFile2String(loginFile);
            PlayerLoginBean loginBean = gson.fromJson(loginBeanJson, PlayerLoginBean.class);
            if (loginBean != null) {
                loginDayCount = String.valueOf(loginBean.getLoginDayCount());
            }
        }
        if (FileUtils.isFileExists(playingFile)) {
            String playingJson = FileUtils.readFile2String(playingFile);
            PlayerPlayingBean playingBean = gson.fromJson(playingJson, PlayerPlayingBean.class);
            long playing = 0;
            PlayerPlayingBean.ChessPlayingBean maxChessPlayingBean = null;
            for (PlayerPlayingBean.ChessPlayingBean chessPlayingBean : playingBean.getChessPlayingList()) {
                playing += chessPlayingBean.getPlayingCount();
                if (maxChessPlayingBean == null || chessPlayingBean.getPlayingCount() > maxChessPlayingBean.getPlayingCount()) {
                    maxChessPlayingBean = chessPlayingBean;
                }
            }
            allPlayingCount = String.valueOf(playing);
            if (maxChessPlayingBean != null) maxPlayingChess = maxChessPlayingBean.getChessName();
        }

        String detail =
                "昵称：" + nickName + "\n" +
                        "荣誉标签：" + badge + "\n" +
                        "创建日期：" + createTime + "\n" +
                        "登录天数：" + loginDayCount + "\n" +
                        "对弈总数：" + allPlayingCount + "\n" +
                        "最经常玩：" + maxPlayingChess;
        return detail;
    }
}
